package com.cz.nongyetongv5.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.config.CommonConstants;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class ShouYeImageAdapter extends BaseAdapter {

	private List<Map<String, Bitmap>> images = null;

	private Context context = null;

	private int executeThreadCount = 0;

	public ShouYeImageAdapter(List<Map<String, Bitmap>> images, Context context) {
		this.images = images;
		this.context = context;
	}

	@Override
	public int getCount() {
		// 这里用来做循环滚动
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int position) {
		return images.get(position % images.size());
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = new ImageView(context);
		}

		final ImageView ivImage = (ImageView) convertView;

		ivImage.setImageResource(R.drawable.default_lunbo);

		ivImage.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT));

		ivImage.setAdjustViewBounds(true);

		ivImage.setScaleType(ScaleType.CENTER_CROP);

		final Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {

				ivImage.setImageBitmap((Bitmap) msg.obj);

				super.handleMessage(msg);
			}

		};

		final Map<String, Bitmap> mapData = (Map<String, Bitmap>) getItem(position);

		final String url = mapData.keySet().iterator().next();

		final Bitmap bitmap = mapData.get(url);

		if (bitmap != null) {
			ivImage.setImageBitmap(bitmap);
		} else {

			if (CommonConstants.isNetworkConnected(context)
					&& executeThreadCount < images.size()) {

				new Thread(new Runnable() {

					@Override
					public void run() {

						Message message = new Message();

						message.obj = getHttpBitmap(url);

						mapData.put(url, (Bitmap) message.obj);

						handler.sendMessage(message);

					}
				}).start();

				executeThreadCount++;

			}
		}

		return ivImage;
	}

	// public void setImages(List<Map<String, Bitmap>> images) {
	// this.images = images;
	// this.notifyDataSetChanged();
	// }

	public static Bitmap getHttpBitmap(String url) {
		URL myFileURL;
		Bitmap bitmap = null;
		try {
			myFileURL = new URL(url);
			// 获得连接
			HttpURLConnection conn = (HttpURLConnection) myFileURL
					.openConnection();
			// 设置超时时间为6000毫秒，conn.setConnectionTiem(0);表示没有时间限制
			conn.setConnectTimeout(6000);
			// 连接设置获得数据流
			conn.setDoInput(true);
			// 不使用缓存
			conn.setUseCaches(false);
			// 这句可有可无，没有影响
			conn.connect();
			// 得到数据流
			InputStream is = conn.getInputStream();
			// 解析得到图片
			bitmap = BitmapFactory.decodeStream(is);
			// 关闭数据流
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;

	}

}
